﻿using Super_Management_Mayhem.Content.Spritesheet;
using Transformable_Engine_v2.Engine.Animation.ScaleAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Menu.CreateAvatarColor
{
    public class AvatarColorMenu : BaseMenuScreen<MainMenu.Menu>
    {
        public static readonly Color[] AvailableColors = new[] { 
                                                                    Color.DarkRed,
                                                                    Color.FireBrick,
                                                                    Color.Red,
                                                                    Color.OrangeRed,
                                                                    Color.Orange,
                                                                    Color.Gold,
                                                                    Color.Yellow,
                                                                    Color.GreenYellow,
                                                                    Color.Green,
                                                                    Color.DarkGreen,
                                                                    Color.Lime,
                                                                    Color.SpringGreen,
                                                                    Color.MediumSeaGreen,
                                                                    Color.DeepSkyBlue,
                                                                    Color.DodgerBlue,
                                                                    Color.Indigo,
                                                                    Color.Purple,
                                                                    Color.MediumVioletRed,
                                                                    Color.DeepPink,
                                                                    Color.Crimson,
                                                                    Color.DarkGray,
                                                                    Color.Gray,
                                                                    Color.White,
                                                                    Color.SaddleBrown
                                                                };

        private const int SELECTION_TIME = 200;
        private const int VISIBLE_COLOR_LEVELS = 6;
        private const int WHEEL_RADIUS = 14;

        private ItemWheel _wheel;

        public AvatarColorMenu(TE_Vector2 size, int selectedIndex)
        {

            var colorSquares = new ImageContainer[AvailableColors.Length];
            for (int i = 0; i < AvailableColors.Length; i++)
            {
                colorSquares[i] = new ImageContainer {
                                                          Sprite = MenuSheet.GetSprite(MenuSheet.Menu.ColorSquare),
                                                          Origin = TE_Rectangle.Alignment.CenterMiddle,
                                                          Scale = new TE_Vector2(3, 3),
                                                          Color = AvailableColors[i]
                                                      };
            }

            var cursorImage = new ImageContainer {
                                                     Sprite = MenuSheet.GetSprite(MenuSheet.Menu.LetterSelected),
                                                     Origin = TE_Rectangle.Alignment.CenterMiddle,
                                                     Scale = new TE_Vector2(1.5f, 1.5f)
                                                 };
            cursorImage.AddAnimations(new ScaleAnimation(new SinusTrajectory(), new TE_Vector2(1.75f, 1.75f), 800, true, 0));

            _wheel = new ItemWheel(selectedIndex, SELECTION_TIME, VISIBLE_COLOR_LEVELS, WHEEL_RADIUS, cursorImage, colorSquares) {
                                                                                        PositionGridBound = new TE_Vector2(size.X / 2, size.Y / 2 + WHEEL_RADIUS)
                                                                                    };

            var title = Title.Blue("Select a character color", size);

            AddChildren(_wheel, title);

            
        }

        public int CurrentColor
        {
            get
            {
                return _wheel.SelectedItemIndex;
            }
        }


        public override void UpKey(int id)
        {
        }

        public override void DownKey(int id)
        {
        }

        public override void LeftKey(int id)
        {
            _wheel.CycleLeft();
        }

        public override void RightKey(int id)
        {
            _wheel.CycleRight();
        }

        public override void AcceptKey(int id)
        {
            ChangeScreen(-1, MainMenu.Menu.CreateNewAvatar_Icon);
        }

        public override void CancelKey(int id)
        {
            ChangeScreen(-1, MainMenu.Menu.CreateNewAvatar_Name);
        }
    }
}